package com.zuo.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zuo.entity.User;
import com.zuo.model.MyResponse;
import com.zuo.service.UserService;
import com.zuo.util.HrmConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 白白旧维
 * @Date 2022/3/1 17:08
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * go跳转
     * @param go
     * @return
     */
    @RequestMapping("/go")
    public String go(String go){
        if("loginForm.jsp".equals(go)){
            return go;
        }
//        System.out.println("go = " + go);
        return "forward:"+go;
    }

    @RequestMapping("/toLoginForm")
    public String toLoginForm(){
        return "loginForm.jsp";
    }

    /**
     * 主页面
     * @return
     */
    @RequestMapping("/main")
    public String toMain(){
        return "main.jsp";
    }

    @RequestMapping("/toFace")
    public String toFace(){
        return "face/face.jsp";
    }


    /**
     * 测试功能页面
     * @return
     */
    @RequestMapping("/test")
    public String test(){
        return "test.jsp";
    }

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public String toLogin(String username,String password,HttpServletRequest request){
        User loginUser = userService.login(username, password);
        if (loginUser!=null){
            loginUser.setPassword(null);
            request.getSession().setAttribute(HrmConstants.USER_SESSION,loginUser);
            return "forward:main.action";
        }else{
            return "loginForm.jsp";
        }
    }

    @RequestMapping("/user/loginOut")
    @ResponseBody
    public String loginOut(HttpServletRequest request){
        request.getSession().removeAttribute(HrmConstants.USER_SESSION);
        Map<String,Object> map = new HashMap<>();
        map.put("message","成功");
        return JSON.toJSONString(map);
    }

    /**
     * 用户列表展示
     * @param model
     * @return
     */
    @RequestMapping("/user")
    public String user(Model model, @RequestParam(value = "page",defaultValue = "1") int page){
        //定义一个当前页的变量（当前页，页面大小）
        //PageHelper一定要在获取数据库集合之前使用
        PageHelper.startPage(page,HrmConstants.PAGE_DEFAULT_SIZE);

        List<User> users = userService.listUser();

        //PageInfo一定要获取到数据库集合之后
        PageInfo<User> pageInfo = new PageInfo<>(users);
        int up = page-1;
        int down = page+1;
        int last = pageInfo.getLastPage();
        long total = pageInfo.getTotal();

        model.addAttribute("up",up);//上一页
        model.addAttribute("page",page);//当前页
        model.addAttribute("down",down);//下一页
        model.addAttribute("last",last);//最后一页
        model.addAttribute("total",total);//数据总数
        model.addAttribute("pageSize",HrmConstants.PAGE_DEFAULT_SIZE);//页面大小
        model.addAttribute("users",users);
        model.addAttribute("type","all");
        return "user/user.jsp";
    }

    /**
     * 用户多条件查询
     * 暂时不能多级search/user.action这样访问
     * @param username
     * @param status
     * @return
     */
    @RequestMapping("/searchUser")
    public String searchUser(String username,int status,Model model, @RequestParam(value = "page",defaultValue = "1") int page){
        PageHelper.startPage(page,HrmConstants.PAGE_DEFAULT_SIZE);

        List<User> users = userService.searchUser(username, status);

        //PageInfo一定要获取到数据库集合之后
        PageInfo<User> pageInfo = new PageInfo<>(users);
        int up = page-1;
        int down = page+1;
        int last = pageInfo.getLastPage();
        long total = pageInfo.getTotal();

        model.addAttribute("up",up);//上一页
        model.addAttribute("page",page);//当前页
        model.addAttribute("down",down);//下一页
        model.addAttribute("last",last);//最后一页
        model.addAttribute("total",total);//数据总数
        model.addAttribute("pageSize",HrmConstants.PAGE_DEFAULT_SIZE);//页面大小
        model.addAttribute("users",users);
        model.addAttribute("type","search");
        model.addAttribute("username",username);
        model.addAttribute("status",status);
        return "user/user.jsp";
    }

    /**
     * 跳转用户信息修改页面/
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/toUpdateUser")
    public String toUpdateUser(int id,Model model){
        User user = userService.getOne(id);
        model.addAttribute("user",user);
        return "user/showUpdateUser.jsp";
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @RequestMapping("/updateUser")
    public String updateUser(User user, Model model, HttpServletRequest request) throws UnsupportedEncodingException {
        int result = userService.updateUser(user);

        if(result!=0){
            model.addAttribute("info","成功修改了"+result+"条数据");
        }else{
            model.addAttribute("info","修改失败");
        }
        return "forward:user.action";
    }

    @RequestMapping("/removeUser")
    public String removeUser(String ids,Model model){
        int result = userService.removeUsers(ids);
        model.addAttribute("info","成功删除了"+result+"条记录");
        return "forward:user.action";
    }

    @RequestMapping("/toAddUser")
    public String toAddUser(){
        return "user/showAddUser.jsp";
    }

    @RequestMapping("/addUser")
    public String addUser(User user,Model model){
        User userByUsername = userService.getUserByUsername(user.getUsername());
        if(userByUsername!=null){
            model.addAttribute("info","添加失败(登录名已存在)");
            return "forward:toAddUser.action";
        }
        int result = userService.addUser(user);
        if(result!=0){
            model.addAttribute("info","添加成功");
        }else{
            model.addAttribute("info","添加失败");
        }
        return "forward:user.action";
    }

    @RequestMapping("/toEditPassword")
    public String toEditPassword(){
        return "editPassword/editPassword.jsp";
    }

    @RequestMapping(value = "/user/editPassword",method = RequestMethod.POST)
    @ResponseBody
    public String editPassword(String username,String oldPassword,String newPassword){
        int result = userService.updatePassword(username,oldPassword,newPassword);
        Map<String,Object> map = new HashMap<>();
        if(result!=0){
            map.put("message","success");
        }else{
            map.put("message","error");
        }
        String json = JSON.toJSONString(map);
        return json;
    }


}
